<?php


namespace app\api\controller;


use app\api\model\SysOperLog;
use app\common\controller\Api;
use app\common\controller\OperationLog;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\Request;

class OperLog extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedAuth = [];
    protected $noNeedToken = [];

    /**
     * 获取操作日志
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function lists()
    {
        // 定义日志参数
        $options = array(
            'title' => '获取操作日志列表',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
            'token' => Request::instance()->header('token')
        );
        $where = Request::instance()->param('where/a') ?? [];
        $page = Request::instance()->param('page') ?? '0,10';
        $where = self::build_params($where);
        $field = [
            'log_id', 'title', 'type', 'oper_name', 'oper_location', 'oper_ip', 'status', 'oper_time'
        ];
        $operLogLists = (new SysOperLog)
            ->field($field)
            ->where($where)
            ->select();
        $pageOperLogLists = (new SysOperLog)
            ->field($field)
            ->page($page)
            ->where($where)
            ->select();
        OperationLog::OperSuccess($where, $options, 'R', $pageOperLogLists);
        $this->success('success', ['code' => 200, 'data' => ['lists' => $pageOperLogLists, 'total' => count($operLogLists)]]);
    }

    public function detail()
    {
        // 定义日志参数
        $options = array(
            'title' => '获取操作日志详情',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
            'token' => Request::instance()->header('token')
        );
        $param = Request::instance()->param() ?? [];
        $validRes = $this->validate($param, 'SysOperLog.detail');
        if ($validRes !== true) {
            OperationLog::OperError($param, $options, 'R', $validRes);
            $this->error('error', ['code' => 201, 'data' => $validRes]);
        }
        $field = [
            'title', 'open_url', 'oper_name',
            'oper_location', 'oper_ip', 'request_method', 'method',
            'oper_name', 'json_result', 'status', 'oper_time'
        ];
        $sysOperLog = (new SysOperLog)
            ->field($field)
            ->where('log_id', '=', $param['log_id'])
            ->find();
        OperationLog::OperSuccess($param, $options, 'R', [$sysOperLog]);
        $this->success('success', ['code' => 200, 'data' => $sysOperLog]);
    }

    /**
     * 清空操作日志
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function clear_oper_log()
    {
        // 定义日志参数
        $options = array(
            'title' => '清空操作日志',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
            'token' => Request::instance()->header('token')
        );
        Db::startTrans();
        try {
            Db::query('truncate table lm_sys_oper_log');
            Db::commit();
            OperationLog::OperSuccess([], $options, 'D', '操作成功!');
            $this->success('success', ['code' => 200, 'data' => '操作成功!']);
        } catch (Exception $e) {
            OperationLog::OperError([], $options, 'D', $e->getMessage());
            $this->error('error', ['code' => 202, 'data' => $e->getMessage()], 202);
        }
    }
}